package com.ece.friendly.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ece.friendly.service.ArticleService;
import com.ece.friendly.service.FriendService;

@Controller
public class ArticleController {
	@Autowired
	private ArticleService articleService;
	
	@Autowired
	private FriendService friendService;

	@RequestMapping(value = "article/article.view", method = RequestMethod.GET)
	public String home(Locale locale, Model model,HttpServletRequest request, HttpServletResponse response) {
		model.addAttribute("articles", articleService.getAllArticle());
		return "article";
	}
	
	@RequestMapping(value = "article/add.json", method = RequestMethod.POST)
	@ResponseBody  
	public HashMap<String, Object> add(HttpServletRequest request, HttpServletResponse response) {
		HashMap<String, Object> map=new HashMap<String,Object>();
		
		String login=null;
	    for(Cookie c: request.getCookies()) {
	        if(c.getName().equals("login")) login=c.getValue();
	     }
	    String event="";
	    String designation="";
	    String conseil="";FileItem photo=null;
		int etat = 0;
		float prix = 0;
	    if(login!=null) 
	    {
	        
	        try 
	        {
	        	List<FileItem> items = new ServletFileUpload(new DiskFileItemFactory()).parseRequest(request);
	        	
	            for (FileItem item : items) {
	            	if(item.getFieldName().equals("event"))
	            		event=item.getString();
	            	else if(item.getFieldName().equals("designation"))
	            		designation=item.getString();
	            	else if(item.getFieldName().equals("conseil"))
	            		conseil=item.getString();
	            	else if(item.getFieldName().equals("etat"))
	            		etat=Integer.parseInt(item.getString());
	            	else if(item.getFieldName().equals("prix"))
	            		prix=Float.parseFloat(item.getString());
	            	else if(item.getFieldName().equals("photo"))
	            		photo=item;
	               
	            }
	            long currentDate=new Date().getTime();
		        if(articleService.addArticle(login, event, designation,etat ,conseil,prix,photo,currentDate,request.getSession().getServletContext().getRealPath("/")))
		        	map.put("article", articleService.getArticle(login, currentDate));
		        else
		        	map.put("error", "Erreur lors de l'enregistrement de l'article.");	

			} catch (FileUploadException e1) {
				map.put("error", "Erreur lors du telechargement de la photo.");	
				e1.printStackTrace();
			}
	    }
		return map;
	}
	
}
